/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.usuario;

import control.ChamadoControl;
import control.Sessao;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import model.Chamado;
import model.Pessoa;
import model.Resposta;
import util.CellRenderer;
import view.IJIFSimpleFactory;
import view.admin.JDResponderEAAdmin;
import view.admin.JDVisualizarChamAtr;

/**
 *
 * @author Felipe
 */
public class JIFChamadoUsuario extends JInternalFrame implements IJIFSimpleFactory {

    private Pessoa usuarioLogado;
    private ChamadoControl controller;
    
    public JIFChamadoUsuario() {
        this.usuarioLogado = Sessao.getInstance().getUsuarioLogado();
        controller = ChamadoControl.getControl();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlEA = new javax.swing.JPanel();
        pnlEASuperior = new javax.swing.JPanel();
        pnlBusca = new javax.swing.JPanel();
        cbbBusca = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        txtBusca = new javax.swing.JTextField();
        pnlJtb = new javax.swing.JPanel();
        jtbSuperior = new javax.swing.JToolBar();
        btnVisualizarEA = new javax.swing.JButton();
        btnResponderEA = new javax.swing.JButton();
        pnlSuperiorCentro = new javax.swing.JPanel();
        pnlSupCenDiv = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEA = new javax.swing.JTable();
        pnlEAOeste = new javax.swing.JPanel();
        pnlEASul = new javax.swing.JPanel();
        pnlEALeste = new javax.swing.JPanel();
        pnlEncerrados = new javax.swing.JPanel();
        pnlEncSuperior = new javax.swing.JPanel();
        pnlEncBusca = new javax.swing.JPanel();
        cbbEncBusca = new javax.swing.JComboBox();
        btnEncBuscar = new javax.swing.JButton();
        txtEncBusca = new javax.swing.JTextField();
        pnlEncJtb = new javax.swing.JPanel();
        jtbEncSup = new javax.swing.JToolBar();
        btnVisualizarEncerrado = new javax.swing.JButton();
        pnlEncSupCen = new javax.swing.JPanel();
        pnlEncSupCenDiv = new javax.swing.JPanel();
        pnlEncSul = new javax.swing.JPanel();
        pnlEncLeste = new javax.swing.JPanel();
        pnlEncOeste = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblEncerrados = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setTitle("Controle de chamado");
        setPreferredSize(new java.awt.Dimension(745, 586));

        pnlEA.setLayout(new java.awt.BorderLayout());

        pnlEASuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlEASuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlEASuperior.setLayout(new java.awt.BorderLayout());

        pnlBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlBusca.setLayout(new javax.swing.BoxLayout(pnlBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(cbbBusca);

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnBuscar.setToolTipText("Procurar");
        btnBuscar.setMaximumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setMinimumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setOpaque(false);
        btnBuscar.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlBusca.add(btnBuscar);

        txtBusca.setText("jTextField1");
        txtBusca.setMargin(new java.awt.Insets(200, 20, 20, 20));
        txtBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        txtBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(txtBusca);

        pnlEASuperior.add(pnlBusca, java.awt.BorderLayout.EAST);

        pnlJtb.setLayout(new java.awt.BorderLayout());

        jtbSuperior.setBorder(null);
        jtbSuperior.setFloatable(false);
        jtbSuperior.setRollover(true);
        jtbSuperior.setOpaque(false);

        btnVisualizarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarEA.setToolTipText("Visualizar chamado");
        btnVisualizarEA.setFocusable(false);
        btnVisualizarEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarEAActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnVisualizarEA);

        btnResponderEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/responder1.png"))); // NOI18N
        btnResponderEA.setToolTipText("Responder chamado em aberto");
        btnResponderEA.setFocusable(false);
        btnResponderEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnResponderEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnResponderEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnResponderEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResponderEAActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnResponderEA);

        pnlJtb.add(jtbSuperior, java.awt.BorderLayout.CENTER);

        pnlEASuperior.add(pnlJtb, java.awt.BorderLayout.WEST);

        pnlSuperiorCentro.setLayout(new javax.swing.BoxLayout(pnlSuperiorCentro, javax.swing.BoxLayout.LINE_AXIS));

        pnlSupCenDiv.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlSupCenDiv.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlSupCenDiv.setOpaque(false);
        pnlSuperiorCentro.add(pnlSupCenDiv);

        pnlEASuperior.add(pnlSuperiorCentro, java.awt.BorderLayout.CENTER);

        pnlEA.add(pnlEASuperior, java.awt.BorderLayout.PAGE_START);

        tblEA.setModel(controller.getChamadosEATableModel());
        tblEA.setRowHeight(20);
        ajustarColunasEmAndamento();
        jScrollPane1.setViewportView(tblEA);

        pnlEA.add(jScrollPane1, java.awt.BorderLayout.CENTER);
        pnlEA.add(pnlEAOeste, java.awt.BorderLayout.WEST);
        pnlEA.add(pnlEASul, java.awt.BorderLayout.SOUTH);
        pnlEA.add(pnlEALeste, java.awt.BorderLayout.EAST);

        jTabbedPane1.addTab("Em aberto", pnlEA);

        pnlEncerrados.setLayout(new java.awt.BorderLayout());

        pnlEncSuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlEncSuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlEncSuperior.setLayout(new java.awt.BorderLayout());

        pnlEncBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setLayout(new javax.swing.BoxLayout(pnlEncBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbEncBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbEncBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbEncBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbEncBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEncBusca.add(cbbEncBusca);

        btnEncBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnEncBuscar.setToolTipText("Procurar");
        btnEncBuscar.setMaximumSize(new java.awt.Dimension(30, 20));
        btnEncBuscar.setMinimumSize(new java.awt.Dimension(30, 20));
        btnEncBuscar.setOpaque(false);
        btnEncBuscar.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlEncBusca.add(btnEncBuscar);

        txtEncBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        txtEncBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        txtEncBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEncBusca.add(txtEncBusca);

        pnlEncSuperior.add(pnlEncBusca, java.awt.BorderLayout.EAST);

        pnlEncJtb.setLayout(new java.awt.BorderLayout());

        jtbEncSup.setBorder(null);
        jtbEncSup.setFloatable(false);
        jtbEncSup.setRollover(true);
        jtbEncSup.setOpaque(false);

        btnVisualizarEncerrado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarEncerrado.setToolTipText("Visualizar chamado encerrado");
        btnVisualizarEncerrado.setFocusable(false);
        btnVisualizarEncerrado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarEncerrado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbEncSup, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarEncerrado, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarEncerrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarEncerradoActionPerformed(evt);
            }
        });
        jtbEncSup.add(btnVisualizarEncerrado);

        pnlEncJtb.add(jtbEncSup, java.awt.BorderLayout.CENTER);

        pnlEncSuperior.add(pnlEncJtb, java.awt.BorderLayout.WEST);

        pnlEncSupCen.setLayout(new javax.swing.BoxLayout(pnlEncSupCen, javax.swing.BoxLayout.LINE_AXIS));

        pnlEncSupCenDiv.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlEncSupCenDiv.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlEncSupCenDiv.setOpaque(false);
        pnlEncSupCenDiv.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlEncSupCen.add(pnlEncSupCenDiv);

        pnlEncSuperior.add(pnlEncSupCen, java.awt.BorderLayout.CENTER);

        pnlEncerrados.add(pnlEncSuperior, java.awt.BorderLayout.PAGE_START);
        pnlEncerrados.add(pnlEncSul, java.awt.BorderLayout.SOUTH);
        pnlEncerrados.add(pnlEncLeste, java.awt.BorderLayout.EAST);
        pnlEncerrados.add(pnlEncOeste, java.awt.BorderLayout.WEST);

        tblEncerrados.setModel(controller.getChamadosEncerradosTableModel());
        tblEncerrados.setRowHeight(20);
        ajustarColunasEncerrados();
        jScrollPane2.setViewportView(tblEncerrados);

        pnlEncerrados.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Encerrados", pnlEncerrados);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVisualizarEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarEAActionPerformed
        visualizarChamadoEA();
    }//GEN-LAST:event_btnVisualizarEAActionPerformed

    private void btnResponderEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResponderEAActionPerformed
        responderChamadoEA();
    }//GEN-LAST:event_btnResponderEAActionPerformed

    private void btnVisualizarEncerradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarEncerradoActionPerformed
         visualizarChamadoEnce();
    }//GEN-LAST:event_btnVisualizarEncerradoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEncBuscar;
    private javax.swing.JButton btnResponderEA;
    private javax.swing.JButton btnVisualizarEA;
    private javax.swing.JButton btnVisualizarEncerrado;
    private javax.swing.JComboBox cbbBusca;
    private javax.swing.JComboBox cbbEncBusca;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jtbEncSup;
    private javax.swing.JToolBar jtbSuperior;
    private javax.swing.JPanel pnlBusca;
    private javax.swing.JPanel pnlEA;
    private javax.swing.JPanel pnlEALeste;
    private javax.swing.JPanel pnlEAOeste;
    private javax.swing.JPanel pnlEASul;
    private javax.swing.JPanel pnlEASuperior;
    private javax.swing.JPanel pnlEncBusca;
    private javax.swing.JPanel pnlEncJtb;
    private javax.swing.JPanel pnlEncLeste;
    private javax.swing.JPanel pnlEncOeste;
    private javax.swing.JPanel pnlEncSul;
    private javax.swing.JPanel pnlEncSupCen;
    private javax.swing.JPanel pnlEncSupCenDiv;
    private javax.swing.JPanel pnlEncSuperior;
    private javax.swing.JPanel pnlEncerrados;
    private javax.swing.JPanel pnlJtb;
    private javax.swing.JPanel pnlSupCenDiv;
    private javax.swing.JPanel pnlSuperiorCentro;
    private javax.swing.JTable tblEA;
    private javax.swing.JTable tblEncerrados;
    private javax.swing.JTextField txtBusca;
    private javax.swing.JTextField txtEncBusca;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
    
    private void visualizarChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, 
                                true, false, chamado, usuarioLogado);
    }
    
    private void responderChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        boolean responder = false;
                
        // Arrumar este método!
        if (chamado.getRespostas() != null) {
            if (!chamado.getRespostas().isEmpty()) {
                if (chamado.getRespostas().get(chamado.getRespostas().size()-1).isAgindo()) {
                    responder = false;
                } else {
                    responder = true;                
                }
            } else {
                responder = true;            
            }
        } else {
            chamado.setRespostas(new ArrayList<Resposta>());
            responder = true;
        }
        
        if (responder == true) {
            JDResponderEAAdmin jdResponderEAAdmin = new JDResponderEAAdmin(null, false, false, chamado, usuarioLogado);
        } else {
            JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica!");
        }
        
    }        
     
    private void visualizarChamadoEnce() {
        Chamado chamado = controller.getChamadosEncerradosTableModel().getChamadoEJB(getLinha(tblEncerrados));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, true, true, chamado, usuarioLogado);
    }
      
    private int getLinha(JTable tbl) {
        return tbl.convertRowIndexToModel(tbl.getSelectedRow());
    }   
    
    private void ajustarColunasEmAndamento() {     
        TableCellRenderer tcr = new CellRenderer();  
                
        tblEA.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEA.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEA.getColumnModel().getColumn(1).setMaxWidth(120);
        tblEA.getColumnModel().getColumn(1).setMinWidth(150);
        tblEA.getColumnModel().getColumn(2).setMinWidth(120);
        tblEA.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEA.getColumnModel().getColumn(3).setMinWidth(180);
        tblEA.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEA.getColumnModel().getColumn(4).setMinWidth(180);
        tblEA.getColumnModel().getColumn(4).setMaxWidth(170);    
    }
    
    private void ajustarColunasEncerrados() {     
        TableCellRenderer tcr = new CellRenderer();  
        
        tblEncerrados.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEncerrados.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEncerrados.getColumnModel().getColumn(1).setMaxWidth(80);
        tblEncerrados.getColumnModel().getColumn(1).setMinWidth(150);
        tblEncerrados.getColumnModel().getColumn(2).setMinWidth(90);
        tblEncerrados.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEncerrados.getColumnModel().getColumn(3).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEncerrados.getColumnModel().getColumn(4).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(4).setMaxWidth(170);    
    }

}
